﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SG.Logic;
using SG.Domain;
using SG.UI.Models;

namespace SG.UI.Controllers
{
    public class OrdenCompraController : Controller
    {
        ExpArticulo exp = new ExpArticulo();

        //
        // GET: /OrdenCompra/

        public ActionResult Index()
        {
            return View(exp.GetAllOrdenCompra());
        }

        //
        // GET: /OrdenCompra/Details/5

        public ActionResult Details(int id)
        {
            OrdenCompra orden = exp.GetOrdenCompra(id);
            TempData["Proveedor"] = orden.Proveedor.Nombre;
            TempData["FechaEmision"] = orden.FechaEmision;

            return View(exp.GetAllDetalleOrdenCompra(id));
        }

        //
        // GET: /OrdenCompra/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /OrdenCompra/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                OrdenCompra entity = new OrdenCompra();
                entity.IdProveedor = int.Parse(HttpContext.Request["IdProveedor"]);

                entity.FechaEmision = DateTime.Now;                
                entity.Estado = "Emitida";

                if (collection["IdArticulo"] != null)
                {
                    string[] idArticuloList = collection["IdArticulo"].Split(',');
                    string[] cantidadList = collection["Cantidad"].Split(',');
                    for (int i = 0; i < idArticuloList.Length; i++)
                    {
                        DetalleOrdenDeCompra doc = new DetalleOrdenDeCompra();
                        doc.Cantidad = int.Parse(cantidadList[i]);
                        doc.IdArticulo = int.Parse(idArticuloList[i]);
                        entity.DetalleOrdenDeCompra.Add(doc);
                    }
                }
                else
                    throw new Exception("Debe seleccionar al menos un artículo.");


                exp.SaveOrdenCompra(entity);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                TempData["VMAlert"] = new VMAlert() { Title = "Err", Body = ex.Message };
                return View();
            }
        }
        
        //
        // GET: /OrdenCompra/Edit/5
 
        public ActionResult Confirm(int id)
        {
            try
            {
                exp.ConfirmOrdenCompra(id);
             
            }
            catch (Exception ex)
            {
                TempData["VMAlert"] = new VMAlert() { Title = "Err", Body = ex.Message };     
            }
            return RedirectToAction("Index");
        }

        //
        // GET: /OrdenCompra/Delete/5
 
        public ActionResult Anular(int id)
        {
            try
            {
                exp.AnularOrdenCompra(id);

            }
            catch (Exception ex)
            {
                TempData["VMAlert"] = new VMAlert() { Title = "Err", Body = ex.Message };
            }
            return RedirectToAction("Index");
        }

        public ActionResult Generate(int id)
        {
            try
            {
                exp.GenerateOrdenCompra(id);

            }
            catch (Exception ex)
            {
                TempData["VMAlert"] = new VMAlert() { Title = "Err", Body = ex.Message };
            }
            return RedirectToAction("Index");
        }
    }
}
